<template>
	<el-dialog v-model="visible" :title="!dataForm.id ? 'Add新增' : 'Update修改'" width="70%" :close-on-click-modal="false">
		<el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="160px"
			@keyup.enter="submitHandle()">
			<el-row class="search-row">
				<el-col :span="12">
					<el-form-item prop="name" label="ClientName 客户名字">
						<el-input v-model="dataForm.name" placeholder=""></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item prop="mobile" label="TEL 电话号码">
						<el-input v-model="dataForm.mobile" placeholder=""></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row class="search-row">
				<el-col :span="12">
					<el-form-item prop="market" label="Market 市场">
						<el-input v-model="dataForm.market" placeholder=""></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item prop="business" label="Business 生意">
						<el-input v-model="dataForm.business" placeholder=""></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row class="search-row">
				<el-col :span="12">
					<el-form-item prop="salesName" label="Ghana Sales 销售">
						<el-select v-model="dataForm.salesId" filterable placeholder="please input a keyword">
							<el-option v-for="item in salesOptions" :key="item.id" :label="item.realName"
								:value="item.id" />
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item prop="csName" label="Ghana CS 客服">
						<el-select v-model="dataForm.csId" filterable placeholder="please input a keyword">
							<el-option v-for="item in csOptions" :key="item.id" :label="item.realName"
								:value="item.id" />
						</el-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row class="search-row">
				<el-col :span="12">
					<el-form-item prop="shopNo" label="ShopNo 店铺号码">
						<el-input v-model="dataForm.shopNo" placeholder=""></el-input>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">Cancel取消</el-button>
			<el-button type="primary" @click="submitHandle()">Confirm确定</el-button>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus/es'
import { useClientApi, useClientSubmitApi } from '@/api/client'
import { queryUserInfo } from '@/api/user'

const emit = defineEmits(['refreshDataList'])

const visible = ref(false)
const dataFormRef = ref()
const dataForm = reactive({
	name: '',
	mobile: '',
	market: '',
	business: '',
	salesId: '',
	csId: ''
})

const dataRules = ref({
	name: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	mobile: [{ required: true, message: '必填项不能为空', trigger: 'blur' }]
})

const init = (id?: number) => {
	visible.value = true
	dataForm.id = ''

	// 重置表单数据
	if (dataFormRef.value) {
		dataFormRef.value.resetFields()
	}

	// id 存在则为修改
	if (id) {
		getClient(id)
	}
}

const getClient = (id: number) => {
	useClientApi(id).then(res => {
		Object.assign(dataForm, res.data)
	})
}

// 销售、客服选项下拉
const salesOptions = ref([]), csOptions = ref([]);
const getUsersList = (type: string) => {
	let req = {
		page: 1,
		limit: 100,
		roleName: type
	}
	queryUserInfo(req).then(res => {
		switch (type) {
			case 'SALES': salesOptions.value = res.data.list; break;
			case 'CS': csOptions.value = res.data.list; break;
		}
	})
}
getUsersList('SALES');
getUsersList('CS');

// 表单提交
const submitHandle = () => {
	dataFormRef.value.validate((valid: boolean) => {
		if (!valid) {
			return false
		}
		useClientSubmitApi(dataForm).then((res) => {
			ElMessage.success({
				message: '操作成功',
				duration: 500,
				onClose: () => {
					visible.value = false
					emit('refreshDataList')
				}
			})
		})
	})
}

defineExpose({
	init
})
</script>
